from openai import OpenAI
from dotenv import load_dotenv
import os

load_dotenv()

client = OpenAI(
	base_url="https://ai.gitee.com/v1",
	api_key=os.getenv("api_key"),
)

response = client.chat.completions.create(
	messages=[
		{
			"role": "system",
			"content": "You are a helpful and harmless assistant. You should think step-by-step."
		},
		{
			"role": "user",
			"content": [
				{
					"type": "image_url",
					"image_url": {
						"url": "https://example.com/image.jpg" # 需替换为实际的图片 URL
					}
				},
				{
					"type": "text",
					"text": "Please describe this image" # 需替换为实际的文本提示内容
				}
			]
		}
	],
	model="Qwen2-VL-72B",
	stream=True,
	max_tokens=1024,
	temperature=0.2,
	top_p=1,
	extra_body={
		"top_k": -1,
	},
	frequency_penalty=1.2,
)

fullResponse = ""
print("Response:")
for chunk in response:
    if not chunk.choices:
        continue
    if not hasattr(chunk.choices[0], 'delta') or chunk.choices[0].delta is None:
        continue 
    delta = chunk.choices[0].delta
    if hasattr(delta, 'reasoning_content') and delta.reasoning_content:
        fullResponse += delta.reasoning_content
        print(f"\033[90m{delta.reasoning_content}\033[0m", end="", flush=True)
    elif hasattr(delta, 'content') and delta.content:
        fullResponse += delta.content
        print(delta.content, end="", flush=True)